package com.study_online.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.study_online.utils.AliPayUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AliPayConfig {
    private String appId;
    private String appPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;

    public AliPayConfig(){

    }

    private static class AliPayConfigInstance{
        private static final AliPayConfig alipayConfig = new AliPayConfig();
    }

    public static AliPayConfig getInstance(){
        return AliPayConfigInstance.alipayConfig;
    }

    @PostConstruct
    public void init1() {
        // 设置参数（全局只需设置一次）
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com/gateway.do";
        config.signType = "RSA2";
        config.appId = this.appId;
        config.merchantPrivateKey = this.appPrivateKey;
        config.alipayPublicKey = this.alipayPublicKey;
        config.notifyUrl = this.notifyUrl;
        Factory.setOptions(config);

        //暂时保存config
        AliPayUtils.getAliPay(config);
        System.out.println("=======支付宝SDK初始化成功=======");
    }
//    @PostConstruct
//    public void init(){
//        AliPayConfig aliPayConfig = new AliPayConfig();
//        aliPayConfig.setAppId(appId);
//        aliPayConfig.setAppPrivateKey(appPrivateKey);
//        aliPayConfig.setAlipayPublicKey(alipayPublicKey);
//        aliPayConfig.setNotifyUrl(notifyUrl);
//        AliPayUtils.getAliPay(aliPayConfig);
//    }

}
